﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

namespace CalmNine.Framework
{
    /// <summary>
    /// TextMeshPro扩展类
    /// </summary>
    public static class TMPExtension
    {
        /// <summary>
        /// 获取内容对应的适合高度
        /// </summary>
        /// <param name="_Text"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static float GetPreferredHeight(this TMP_Text _Text, string content)
        {
            float maxWidth = _Text.rectTransform.sizeDelta.x;
            Vector2 vec = _Text.GetPreferredValues(content, maxWidth, Mathf.Infinity);
            return vec.y;
        }

        /// <summary>
        /// 设置textmeshpro的合适高度
        /// </summary>
        /// <param name="_Text"></param>
        /// <param name="content"></param>
        public static void SetPreferredHeight(this TMP_Text _Text, string content)
        {
            Vector2 origin = _Text.rectTransform.sizeDelta;
            _Text.rectTransform.sizeDelta = new Vector2(origin.x, _Text.GetPreferredHeight(content));
        }

        /// <summary>
        /// 添加文字
        /// </summary>
        /// <param name="_Text"></param>
        /// <param name="content"></param>
        public static void Append(this TMP_Text _Text, string content)
        {
            string originText = _Text.text;
            _Text.text = originText + content;
        }
    }
}